package com.fitmanagement.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.fitmanagement.entity.Users;
import com.fitmanagement.mapper.UsersMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;

@Component
public class TokenUtils {
    private static UsersMapper staticUserMapper;

    @Resource
    UsersMapper usersMapper;

    @PostConstruct
    public void setUserService(){
        staticUserMapper = usersMapper;
    }

//    生成token
    public static String createToken(String userId, String sign){
        return JWT.create().withAudience(userId)//将user id 保存到token里面作为载荷
                .withExpiresAt(DateUtil.offsetHour(new Date(),2))//2小时后token过期
                .sign(Algorithm.HMAC256(sign));//以password作为token的密钥
    }

    //获取当前登录的用户信息
    public static Users getCurrentUser(){
        try{
            HttpServletRequest request= (HttpServletRequest) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String token=request.getHeader("token");
            if (StrUtil.isNotBlank(token)){
                String userId= JWT.decode(token).getAudience().get(0);
                return staticUserMapper.selectById(Integer.valueOf(userId));
            }
        }
        catch (Exception e){
            return null;
        }
        return null;
    }
}
